package middleware

import (
	"fmt"
	"github.com/cuiyuanxin/airuisi-admin/pkg/app"
	"github.com/cuiyuanxin/airuisi-admin/pkg/errcode"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

func RecoveryMiddleware(loger *zap.Logger) gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				loger.Panic(fmt.Sprintf("panic recover err: %v", err))
				// 可以在这里添加发送邮件扩展或者其他的扩展
				app.NewResponse(c).ToError(errcode.ServerError, nil)
				c.Abort()
			}
		}()
		c.Next()
	}
}
